<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 添加角色按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <!-- 角色列表 -->
      <el-table :data="roleList" border stripe>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template v-slot="scope">
            <el-row
              :class="['bdbottom', i1 === 0 ? 'bdtop' : '', 'vcenter', 'nei']"
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
            >
              <!-- 一级 -->
              <el-col :span="5">
                <el-tag
                  closable
                  @close="removeRightById(scope.row, item1.id)"
                  >{{ item1.authName }}</el-tag
                >
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级、三级 -->
              <el-col :span="19">
                <!-- for  -->
                <el-row
                  :class="[i2 === 0 ? '' : 'bdtop', 'vcenter']"
                  v-for="(item2, i2) in item1.children"
                  :key="item2.id"
                >
                  <!-- 二级 -->
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRightById(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级 -->
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      closable
                      @close="removeRightById(scope.row, item3.id)"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

            <!-- <pre>
                    {{scope.row}}
                </pre> -->
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" size="small"
              >编辑</el-button
            >
            <el-button type="danger" icon="el-icon-delete" size="small"
              >删除</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="small"
              @click="showSetRightDiglog(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="setRightDiglogVisible"
      width="50%"
    @close="defKeys = []"
    >
    <!-- 树形控件 -->
      <el-tree
        :data="rightsList"
        :props="treeProps"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="this.defKeys"
        ref="treeRef"
      ></el-tree>

      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDiglogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRights"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getroleListAjax,
  deleteRolesAjax,
  getRightDiglogAjax,
  allotRightsAjax
} from "@/utils/api";
export default {
  data() {
    return {
      // 角色列表数据
      roleList: [],
      //   控制分配权限对话框的显示影藏
      setRightDiglogVisible: false,
      //   所有权限数据
      rightsList: [],
    //   树形控件的属性绑定对象
      treeProps:{
        children:'children',
        label:'authName'
      },
    //   默认选中的节点id值数组
      defKeys:[],
    // 即将分配权限的角色id
    roleID:''
    };
  },
  methods: {
    // 获取所有的角色列表
    async getRoleList() {
      const { data: res } = await getroleListAjax();
      this.roleList = res.data;
    //   console.log(this.roleList);
    },
    // 根据id删除对应权限
    async removeRightById(role, rightId) {
      // 弹框提示用户是否删除
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户权限, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      if (confirmResult !== "confirm") {
        return this.$message.info("取消了删除");
      }

      // 发起删除权限的请求
      const { data: res } = await deleteRolesAjax(role.id, rightId);
      this.$message.success(res.meta.msg);
      // this.getRoleList();
      role.children = res.data;
    },
    // 展示分配权限对话框
    async showSetRightDiglog(role) {
        this.roleID = role.id;
      // 获取到所有权限的数据
      const { data: res } = await getRightDiglogAjax();
      // 获取到的数据赋值给data中rightsList
      this.rightsList = res.data;
      // 递归获取三级节点的id 
      this.getLeafKeys(role,this.defKeys);
      this.setRightDiglogVisible = true;
    },
    // 递归函数  获取角色中三级目录的id值 保存到defKeys
    getLeafKeys(node,arr){
        // 如果当前节点不包含children 则是三级节点
        if(!node.children){
            return arr.push(node.id)
        }

        node.children.forEach(item => 
        this.getLeafKeys(item,arr))
    },
    // 点击为角色分配权限
    async allotRights(){
        // 获取所有半选中和已选中的id值 存放到KEYS中
        const KEYS = [
            ...this.$refs.treeRef.getCheckedKeys(),
            ...this.$refs.treeRef.getHalfCheckedKeys()
        ]

        const IDSTR = KEYS.join(',');
        console.log(IDSTR);
        const {data:res} = await allotRightsAjax(this.roleID,IDSTR);
        this.$message.success(res.meta.msg);
        this.getRoleList();
        this.setRightDiglogVisible = false;
    }
  },
  created() {
    this.getRoleList();
  },
};
</script>

<style lang='less' scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}

.vcenter {
  display: flex;
  align-items: center;
}
.nei {
  margin: 0 30px;
}
</style>